/*
 * CSocketUtils.h
 *
 *  Created on: 2011-9-7
 *      Author: stargui
 */

#ifndef CSOCKETUTILS_H_
#define CSOCKETUTILS_H_
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "../CommonLibrary.h"
namespace Common
{
class CSocketUtils
{

public:

	CSocketUtils();

	virtual ~CSocketUtils();

public:

	static int OpenUdpSocket();

	static int OpenTcpSocket();

	static int OpenRawSocket();

	static bool SetNonBlockSocket(int handle);

	static int SetSocketBuffer(int handle, int rcvbuf, int sndbuf);

	static int SetBroadcastSocket(int handle, int broadcast);

	static bool ListenTcpSocket(int handle, unsigned short port, in_addr_t addr = INADDR_ANY);

	static bool ListenUdpSocket(int handle, unsigned short port, in_addr_t addr = INADDR_ANY);

	static int SetTcpSocketTimeout(int handle, int idle, int interval, int times);

	static int AcceptTcpSocket(int handle, int timeout);

	static int ConnectTcpSocket(int handle, const char *address, unsigned short port);

	static void CloseSocket(int handle);

public:

	static int Send(int handle, const void *buf, int len, int flags);

	static int SendTo(int handle, const void *buf, int len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);

	static int Recv(int handle, void *buf, int len, int flags);

	static int RecvFrom(int handle, void *buf, int len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);

public:

	static void ParseAddress(std::string value, struct sockaddr &src_addr);

	static void ParseAddress(struct sockaddr src_addr, char *buffer, int len);

	static void ParseAddress(std::string value, std::string &address, unsigned short &port);

};
}
#endif /* CSOCKETUTILS_H_ */
